package com.yige.web.action

import com.mongodb.BasicDBObject
import com.yige.common.DateUtil
import com.yige.service.mongoService.MongoActionIncomeService
import com.yige.service.mongoService.MongoCostService
import com.yige.service.mongoService.MongoIncomeService
import com.yige.service.mongoService.MongoLessonStuService
import com.yige.service.mongoService.MongoStudentService
import com.yige.service.mongoService.MongoTeacherService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.util.StringUtils
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody

import java.text.SimpleDateFormat

/**
 * Created by sunhao on 2016/3/31 0031.
 */
@Controller
@RequestMapping("appStudent")
class AppStudentAction {

    @Autowired
    MongoTeacherService teacherService

    @Autowired
    MongoLessonStuService lessonStuService

    @Autowired
    MongoStudentService studentService

    @Autowired
    MongoCostService costService

    @Autowired
    MongoIncomeService incomeService

    @Autowired
    MongoActionIncomeService actionIncomeService

    @RequestMapping('findCost')
    @ResponseBody
    def findStudy(String studentId, String lessonId, String startDate, String endDate) {
        def query = new BasicDBObject(), list = new ArrayList()
        def sdf = new SimpleDateFormat("yyyy-MM-dd")
        if (!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)) {
            query.append("date", [$gte: sdf.parse(startDate), $lte: sdf.parse(endDate)] as BasicDBObject)
        } else if (!StringUtils.isEmpty(startDate) && StringUtils.isEmpty(endDate)) {
            query.append("date", [$gte: sdf.parse(startDate)] as BasicDBObject)
        } else if (StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)) {
            query.append("date", [$lte: sdf.parse(endDate)] as BasicDBObject)
        }
        if (!StringUtils.isEmpty(studentId)) {
            query.append("student", studentId)
        }
        if (!StringUtils.isEmpty(lessonId)) {
            query.append("lesson", lessonId)
        }
        def cost = costService.findListByQueryDesDate(query), count = 0
        cost.each {
            def obj = ["date"  : DateUtil.formatDate(it.date as Date), "teacher": this.teacherService.findOneById(it.teacher).name, "flag": false, "pay": it.pay,
                       "lesson": this.lessonStuService.findOneById(it.lesson).name, "student": this.studentService.findOneById(it.student).name]
            if (it.pay == 0) {
                obj.flag = true
            }
            list.add(obj)
            count = count + it.pay
        }
        return [msg: true, data: list, sum: list.size(), count: count]
    }

    @RequestMapping("findLessons")
    @ResponseBody
    def findLessons(String studentId) {
        def list = new ArrayList(), obj = new BasicDBObject()
        def lessons = studentService.findAllLessonForStudent(studentId)
        lessons.each {
            def n = it.n
            def str = "剩余课程" + n + "节!"
            if (n < 0) {
                n = -n
                str = "超出课程" + n + "节，请及时交费!"
            }
            obj = [id: it.id, name: it.name, str: str] as BasicDBObject
            list.add(obj)
        }
        return [msg: true, data: list]
    }

    @RequestMapping("findIncome")
    @ResponseBody
    def findIncome(String studentId, String actionId, Integer num, String startDate, String endDate) {
        def query = new BasicDBObject(), list = new ArrayList(), obj = new BasicDBObject()
        def sdf = new SimpleDateFormat("yyyy-MM-dd")
        if (!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)) {
            query.append("date", [$gte: sdf.parse(startDate), $lte: sdf.parse(endDate)] as BasicDBObject)
        } else if (!StringUtils.isEmpty(startDate) && StringUtils.isEmpty(endDate)) {
            query.append("date", [$gte: sdf.parse(startDate)] as BasicDBObject)
        } else if (StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)) {
            query.append("date", [$lte: sdf.parse(endDate)] as BasicDBObject)
        }
        if (!StringUtils.isEmpty(studentId)) {
            query.append("student", studentId)
        }
        if (!StringUtils.isEmpty(actionId)) {
            query.append("action", actionId)
        }
        if (!StringUtils.isEmpty(num)) {
            query.append("num", num as int)
        }
        def income = incomeService.findListByQueryDesDate(query), count = 0
        income.each {
            obj = [date   : DateUtil.formatDate(it.date as Date), num: it.num, pay: it.total,
                   student: this.studentService.findOneById(it.student).name,
                   action : this.actionIncomeService.findOneById(it.action).name] as BasicDBObject
            list.add(obj)
            count = count + it.total
        }
        return [msg: true, count: count, data: list]
    }

    @RequestMapping("findAction")
    @ResponseBody
    def findAction() {
        def list = new ArrayList(), obj = new BasicDBObject()
        def action = actionIncomeService.findList()
        action.each {
            if (!("期初余额".equals(it.name))) {
                obj = [id: it._id, name: it.name]
                list.add(obj)
            }
        }
        return [msg: true, data: list]
    }

}
